package com.axc.gallery2.edit.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.axc.gallery2.R;
import com.axc.gallery2.edit.EditActivity;
import com.axc.gallery2.edit.ToReleaseBitmap;

/*
 * 把添加点击事件的事件都放进线程
 * */
public class CircleRenderActivity extends Activity {
	
	// 图片内容
	//static int imageID = R.drawable.xigua;
	
	RelativeLayout reLayout;
	
	
	static CircleRenderActivity circlerender;
	
	//主题应用
	public static CircleRenderActivity Talbum;
	public int theme = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 主题
        Talbum = this;
        theme = getSharedPreferences("cons", MODE_PRIVATE).getInt("theme",
                android.R.style.Theme);
        setTheme(theme);
		super.onCreate(savedInstanceState);  
		circlerender=this;
		/**
		 * 获取图片判断大小
		 */
		Bitmap bmp=new ToReleaseBitmap().RelBmp(EditActivity.imageFileNames);//BitmapFactory.decodeFile(EditActivity.imageFileNames);
		//Bitmap bmp = ((BitmapDrawable) getResources().getDrawable(imageID)).getBitmap();
		Display display = getWindowManager().getDefaultDisplay();
		// 设置不显示标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
		/*if (bmp.getWidth() > display.getWidth()||bmp.getWidth()>display.getWidth()-80) {*/
			if (bmp.getWidth() < bmp.getHeight()) {
				// 宽度小于高度
				reLayout = new EditRender(this, display.getWidth() - 20,
						display.getHeight() - 80);
				//view=new CircleRenderView(this, display.getWidth()-80, display.getHeight()-160);
			} else if (bmp.getWidth() > bmp.getHeight()) {
				// 宽度大于高度
				reLayout = new EditRender(this, display.getWidth(), display
						.getHeight() - 240);
				//view=new CircleRenderView(this, display.getWidth(), display.getHeight()-240);
			} else {
				reLayout = new EditRender(this, display.getWidth(), display
						.getWidth());
				//view=new CircleRenderView(this, display.getWidth(), display.getWidth());
			}
			setContentView(reLayout);
	}
	/***
	 * 按下事件
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			showDialog(1);
		}
		return false;
	}

	/***
	 * 对话框
	 */
	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		if (id == 1)
			return this.buildDialog(CircleRenderActivity.this);
		else {
			return null;
		}
	}

	/***
	 * 对话框
	 */
	private Dialog buildDialog(Context context) {

		AlertDialog.Builder builder = new AlertDialog.Builder(context);

		builder.setIcon(R.drawable.ic_launcher);

		builder.setTitle("确定要退出？");

		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TestActivity.this.finish();
				myDialog();
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		return builder.create();
	}

	/***
	 * 对话框
	 */
	public void myDialog() {
		AlertDialog.Builder builder = new Builder(CircleRenderActivity.this);
		builder.setMessage("图片未保存,是否要保存?");
		builder.setTitle("提示：");
		builder.setPositiveButton("是", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				// 保存
				Toast.makeText(CircleRenderActivity.this, "图片已保存",
						Toast.LENGTH_SHORT).show();
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				CircleRenderActivity.this.finish();
			}
		});
		builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				// 保存
				CircleRenderActivity.this.finish();
			}
		});
		builder.create().show();
	}

	Menu menu;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(Menu.NONE, 1, 0, "保存");
		menu.add(Menu.NONE, 2, 1, "取消");
		menu.add(Menu.NONE, 3, 2, "退出");
		this.menu = menu;
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case 1:
			// 保存 图片
			break;
		case 2:
			// 取消
			break;
		case 3:
			showDialog(1);
			break; 
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
